package com.ruoyi.supervise.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BaseBussinessRecord;
import com.ruoyi.supervise.domain.ConstructionPermit;
import com.ruoyi.supervise.domain.ConstructionPermitChangeApply;
import com.ruoyi.supervise.domain.ConstructionPermitChangeApplyFile;
import com.ruoyi.supervise.mapper.ConstructionPermitChangeMapper;
import com.ruoyi.supervise.service.IConstructionPermitChangeService;
import com.ruoyi.supervise.util.FieldStrUtils;
import com.ruoyi.supervise.util.XMLUtil;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: ConstructionPermitChangeServiceImpl
 * @Description: 施工许可变更管理impl
 * @Author wtj
 * @DateTime 2020年4月7日 下午3:25:20
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class ConstructionPermitChangeServiceImpl implements IConstructionPermitChangeService 
{
    @Autowired
    private ConstructionPermitChangeMapper constructionPermitChangeMapper;

	@Override
	public List<ConstructionPermit> selectConPermitChangeExamineList(ConstructionPermit cp) {
		if(StringUtils.isEmpty(cp.getBeginReportTime()) && !StringUtils.isEmpty(cp.getEndReportTime())) {
			cp.setBeginReportTime("1970-01-01");
		}else if(!StringUtils.isEmpty(cp.getBeginReportTime()) && StringUtils.isEmpty(cp.getEndReportTime())) {
			cp.setEndReportTime(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		List<ConstructionPermit> constructionPermitList = constructionPermitChangeMapper.selectConPermitChangeExamineList(cp);
		if(!StringUtils.isEmpty(constructionPermitList)) {
			for(ConstructionPermit c:constructionPermitList) {
	    		if(c!=null && !StringUtils.isEmpty(c.getChangeContentXml())) {
	    			ConstructionPermitChangeApply cpca = (ConstructionPermitChangeApply) XMLUtil.convertXmlStrToObject(ConstructionPermitChangeApply.class, c.getChangeContentXml());
	    			if(cpca!=null) {
	    				c.setUnitEngineeringName(cpca.getUnitEngineeringName());
	    				c.setCpGuid(cpca.getGuid());
	    				c.setProGuid(cpca.getProjectInfoGuid());
	    				c.setConstructUnitId(cpca.getConstructUnitId());
	    				c.setConstructUnit(cpca.getConstructUnit());
	    				c.setTotalCompanyGuid(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 0));
	    				c.setTotalCompanyName(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 1));
	    				c.setCompactPrice(cpca.getCompactPrice());
	    				c.setDevelopmentScope(cpca.getDevelopmentScope());
	    				c.setInChargeDepartmentGuid(cpca.getInChargeDepartmentGuid());
	    				c.setInChargeDepartment(cpca.getInChargeDepartment());
	    			}
	    		}
			}
		}
		return constructionPermitList;
	}

	@Override
	public ConstructionPermitChangeApply conPermitChangeApply(String dataGuid) {
		List<BaseBussinessRecord> bbrList = constructionPermitChangeMapper.selectConPermitChangeExamineDetail(dataGuid);
		ConstructionPermitChangeApply cpca = new ConstructionPermitChangeApply();
		if(bbrList.get(0) != null) {
			cpca = (ConstructionPermitChangeApply) XMLUtil.convertXmlStrToObject(ConstructionPermitChangeApply.class, bbrList.get(0).getChangeContentXml());
			cpca.setMaterials(FieldStrUtils.fileFieldThreePara(bbrList.get(0).getMaterials(), 2, 1));
			cpca.setReportDate(bbrList.get(0).getReportDate());
			cpca.setSurveyUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSurveyUnitName(), 2, 1));
			cpca.setSurveyUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSurveyUnitName(), 2, 0));
			cpca.setDesignUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getDesignUnit(), 2, 1));
			cpca.setDesignUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getDesignUnit(), 2, 0));
			cpca.setTotalCompanyNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 1));
			cpca.setTotalCompanyGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 0));
			cpca.setSupervisionUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSupervisionUnit(), 2, 1));
			cpca.setSupervisionUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSupervisionUnit(), 2, 0));
			if(!StringUtils.isEmpty(bbrList.get(0).getChangeContentXml())) {
				ConstructionPermitChangeApplyFile cpcaf = new ConstructionPermitChangeApplyFile();
				cpcaf.setChekFileName("ChekFileName");
				cpcaf.setReMark("ReMark");
				cpcaf.setAcceName("AcceName");
				cpcaf.setOperator("Operator");
				cpcaf.setCreateDate("CreateDate");
				String nodePath = "/PsDataPacket/CheckFileOpinionS/FDataList/CheckFileOpinion";
				List<ConstructionPermitChangeApplyFile> cpcafList = XmlReadUtil.getConstructionPermitChangeApplyFile(bbrList.get(0).getChangeContentXml(), nodePath, cpcaf);
				if(!StringUtils.isEmpty(cpcafList)) {
					for(ConstructionPermitChangeApplyFile con:cpcafList) {
						con.setAcceName(FieldStrUtils.fileFieldThreePara(con.getAcceName(), 2, 1));
					}
				}
				cpca.setFile(cpcafList);
			}
		}
		return cpca;
	}

	@Override
	public List<BaseBussinessRecord> selectConPermitChangeExamineDetail(String dataGuid) {
		// TODO Auto-generated method stub
		return constructionPermitChangeMapper.selectConPermitChangeExamineDetail(dataGuid);
	}

	@Override
	public List<BaseBussinessRecord> selectConPermitChangeExamineListByDataGuid(String dataGuid) {
		List<BaseBussinessRecord> bbrList = constructionPermitChangeMapper.selectConPermitChangeExamineListByDataGuid(dataGuid);
		for(BaseBussinessRecord bbr:bbrList) {
			bbr.setTotalCompanyGuid(FieldStrUtils.personOrEnterFieldParaNot(bbr.getTotalCompany(), 2, 0));
			bbr.setTotalCompanyName(FieldStrUtils.personOrEnterFieldParaNot(bbr.getTotalCompany(), 2, 1));
			if(bbr!=null && !StringUtils.isEmpty(bbr.getChangeContentXml())) {
				ConstructionPermitChangeApply cpca = (ConstructionPermitChangeApply) XMLUtil.convertXmlStrToObject(ConstructionPermitChangeApply.class, bbr.getChangeContentXml());
    			if(cpca!=null) {
    				bbr.setUnitEngineeringName(cpca.getUnitEngineeringName());
    				bbr.setConstructUnitId(cpca.getConstructUnitId());
    				bbr.setConstructUnit(cpca.getConstructUnit());
    				bbr.setTotalCompanyGuid(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 0));
    				bbr.setTotalCompanyName(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 1));
    			}
			}
		}
		return bbrList;
	}

	@Override
	public ConstructionPermitChangeApply selectConPermitChangeExamineDetailByGuid(String guid) {
		BaseBussinessRecord bbr = constructionPermitChangeMapper.selectConPermitChangeExamineDetailByGuid(guid);
		ConstructionPermitChangeApply cpca = new ConstructionPermitChangeApply();
		if(cpca != null) {
			cpca = (ConstructionPermitChangeApply) XMLUtil.convertXmlStrToObject(ConstructionPermitChangeApply.class, bbr.getChangeContentXml());
			cpca.setMaterials(FieldStrUtils.fileFieldThreePara(bbr.getMaterials(), 2, 1));
			cpca.setReportDate(bbr.getReportDate());
			cpca.setSurveyUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSurveyUnitName(), 2, 1));
			cpca.setSurveyUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSurveyUnitName(), 2, 0));
			cpca.setDesignUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getDesignUnit(), 2, 1));
			cpca.setDesignUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getDesignUnit(), 2, 0));
			cpca.setTotalCompanyNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 1));
			cpca.setTotalCompanyGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getTotalCompany(), 2, 0));
			cpca.setSupervisionUnitNameStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSupervisionUnit(), 2, 1));
			cpca.setSupervisionUnitGuidStr(FieldStrUtils.personOrEnterFieldParaNot(cpca.getSupervisionUnit(), 2, 0));
			if(!StringUtils.isEmpty(bbr.getChangeContentXml())) {
				ConstructionPermitChangeApplyFile cpcaf = new ConstructionPermitChangeApplyFile();
				cpcaf.setChekFileName("ChekFileName");
				cpcaf.setReMark("ReMark");
				cpcaf.setAcceName("AcceName");
				cpcaf.setOperator("Operator");
				cpcaf.setCreateDate("CreateDate");
				String nodePath = "/PsDataPacket/CheckFileOpinionS/FDataList/CheckFileOpinion";
				List<ConstructionPermitChangeApplyFile> cpcafList = XmlReadUtil.getConstructionPermitChangeApplyFile(bbr.getChangeContentXml(), nodePath, cpcaf);
				if(!StringUtils.isEmpty(cpcafList)) {
					for(ConstructionPermitChangeApplyFile con:cpcafList) {
						con.setAcceName(FieldStrUtils.fileFieldThreePara(con.getAcceName(), 2, 1));
					}
				}
				cpca.setFile(cpcafList);
			}
		}
		return cpca;
	}
    
}
